﻿// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

// Version Histroy
// v.1: Mainrelease
// v.1.0.1: New Icon

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace linkcatcher
{
    class Program
    {
        static void printDoc()
        {
            Console.WriteLine(":: Doc");
            Console.WriteLine(": Gets Links out of your File");
            Console.WriteLine(": Usage: [input] [output]");
            Console.WriteLine(": [input] File with mixed content");
            Console.WriteLine(": [output] File to write links to");
        }

        static void Main(string[] args)
        {
            System.Console.WriteLine(":: Linkcatcher v.1.0.1 by Shrimpy");
            System.Console.WriteLine(":: http://code.google.com/p/linkcatcher/");
            System.Console.WriteLine("");

            if (args.Length < 2)
            {
                printDoc();
                return;
            }

            string InputFile = args[0];
            string OutputFile = args[1];

            if (!File.Exists(InputFile))
            {
                System.Console.WriteLine("[Input] have to be a vaild Textfile!");
                return;
            }

            InputFile = Path.GetFullPath(InputFile);
            OutputFile = Path.GetFullPath(OutputFile);

            StreamReader InputStream = new StreamReader(@InputFile);
            StreamWriter OutputStream = new StreamWriter(@OutputFile);

            System.Text.RegularExpressions.Regex Pattern = new System.Text.RegularExpressions.Regex(@"^[\\h\t\\t\\p\:\/\/](www.)?[A-Za-z0-9\/\?_\=]+");

            Console.WriteLine("Start reading and writing input...");
            //OutputStream.Write(":: Linkcatcher v.1.0.1 by Shrimpy\n:: http://code.google.com/p/linkcatcher/");

            int count = 0;
            string Line;
            while ((Line = InputStream.ReadLine()) != null)
            {
                string[] Parts = Line.Split(new Char[] { ' ', '\t' });
                foreach (string Part in Parts)
                {
                    if (Pattern.IsMatch(Part))
                    {
                        
                        OutputStream.Write("\n" + Part);
                        count++;
                        Console.Out.Flush();
                        Console.WriteLine(count + " Link(s) found.");
                    }
                }
            }

            InputStream.Close();
            OutputStream.Close();

            Console.WriteLine("Finished writing data.");
            Console.WriteLine(":: Have Fun.");
        }
    }
}
